package com.toogu.mazhangmen.ui.monitor;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.util.NetWorkUtil;
import com.toogu.mazhangmen.util.UiUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MessageFragment extends Fragment {
    private LinearLayout layout;
    private EquipmentsBean equipmentsBean;

    public MessageFragment(EquipmentsBean equipmentsBean){
        this.equipmentsBean=equipmentsBean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scroll, container, false);
        layout=view.findViewById(R.id.layout);
        getMessage(equipmentsBean);
        return view;
    }

    public void setMessage(EquipmentDetailBean equipmentDetailBean) {
        layout.addView(createText(""));
        layout.addView(createText("设备地址："+equipmentDetailBean.getAddress()));
        layout.addView(createText("启用时间："+equipmentDetailBean.getStart_at()));
        layout.addView(createText(""));
//        layout.addView(presenter.createText("设备序列："+equipmentsBean.getCode()));
//        layout.addView(presenter.createText("设备账号："+equipmentsBean.getUsername()));
//        layout.addView(presenter.createText("设备密码："+equipmentsBean.getPassword()));
        layout.addView(createText("设备型号："+equipmentDetailBean.getType()));
        layout.addView(createText("设备温度："+equipmentDetailBean.getTemperature()+"°C"));
        layout.addView(createText("设备状态："+equipmentDetailBean.getStatus_text()));//正常，离线，异常
        layout.addView(createText("网络速率："+equipmentDetailBean.getNetwork_speed()+"KB/S"));
        layout.addView(createText("网络模式："+equipmentDetailBean.getNetwork_model()));
        layout.addView(createText("挡位控制："+equipmentDetailBean.getGear()));
        layout.addView(createText("电流电压："+equipmentDetailBean.getElectric_current()+"A/"+equipmentDetailBean.getVoltage()+"V"));
        layout.addView(createText("红外状态："+(equipmentDetailBean.getInfrared_status().equals("1")?"正常":"异常")));
        layout.addView(createText("报警灯状态："+(equipmentDetailBean.getAlarm_lamp_status().equals("1")?"正常":"异常")));
        layout.addView(createText(""));
        layout.addView(createText("运维管理员："+equipmentDetailBean.getMaintenance_name()));
        layout.addView(createText("运维电话："+equipmentDetailBean.getMaintenance_mobile()));
    }

    private TextView createText(String content) {
        TextView textView=new TextView(getActivity());
        textView.setTextColor(getResources().getColor(R.color.text_blue));
        textView.setText(content);
        textView.setPadding(UiUtil.dip2px(5),UiUtil.dip2px(5),UiUtil.dip2px(5),0);
        return textView;
    }

    private void getMessage(EquipmentsBean equipmentsBean) {
        NetWorkUtil.getNetWorkUtil().equipmentDetail(getActivity(),equipmentsBean.getId(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseText = response.body().string();
                if (response.isSuccessful()) {
                    EquipmentDetailBean equipmentDetailBean = new Gson().fromJson(responseText, EquipmentDetailBean.class);
                    getActivity().runOnUiThread(() ->setMessage(equipmentDetailBean));
                }
            }
        });
    }

    public class EquipmentDetailBean {
        private String address;//地址
        private String start_at;//
        private String type;//
        private String temperature;//
        private String status_text;//
        private String network_speed;//
        private String network_model;//
        private String gear;//
        private String electric_current;//
        private String voltage;//
        private String infrared_status;//
        private String alarm_lamp_status;//
        private String maintenance_name;//
        private String maintenance_mobile;//

        public String getAddress() {
            return address;
        }

        public String getStart_at() {
            return start_at;
        }

        public String getType() {
            return type;
        }

        public String getTemperature() {
            return temperature;
        }

        public String getStatus_text() {
            return status_text;
        }

        public String getNetwork_speed() {
            return network_speed;
        }

        public String getNetwork_model() {
            return network_model;
        }

        public String getGear() {
            return gear;
        }

        public String getElectric_current() {
            return electric_current;
        }

        public String getVoltage() {
            return voltage;
        }

        public String getInfrared_status() {
            return infrared_status;
        }

        public String getAlarm_lamp_status() {
            return alarm_lamp_status;
        }

        public String getMaintenance_name() {
            return maintenance_name;
        }

        public String getMaintenance_mobile() {
            return maintenance_mobile;
        }
    }


}
